<?php
namespace app\index\controller;
use app\index\Model\User as UserModel;
use app\index\Model\Usouquan as UsouquanModel;
use app\index\model\Urole as UroleModel;
use app\index\model\GrmUserUrole as UserUroleModel;
use think\Controller;
use think\Requist;
use think\Session;
use think\Loader;
use think\Cache;
use think\Db;

class Request extends Controller
 {
    protected $user;
    public $userModel;
    public $usouquanModel;
    public $UroleModel;
    public $userroleModel;
    public function _initialize()
    {
        $this->user = Loader::model('user');
        $this->userModel = new UserModel();
        $this->usouquanModel = new UsouquanModel();
        $this->uroleModel = new UroleModel();
        $this->userroleModel = new UserUroleModel();
    }
    //###################注册##########################################
    //用户名
    public function name()
    {
        $request = request();
        $a = trim($request->param('name'));
        if ($a==''){
            return json_encode(0);
        } else {
            $name = Db::name('user')->where('name',$a)->select();
            if (empty($name)){
                return json_encode(1);
            }else{
                return json_encode(2);
            }   
        } 
    }
    //邮箱
    public function email()
    {
        $result = '';
        $email = trim(request()->param('email'));
        $where = ['enter_num'=>$email];
        $result = $this->validate(
            ['email' => $email],
            ['email' => 'email']
            );
        if (true !== $result) {
           $result = 'buhe';
        }
        if($this->usouquanModel->souquanSelect($where)){
                return $re='cunzai'. $result;
        }else{
            return $re = 'buzai'. $result;
        };
    }
    //手机号
    public function number()
    {
        $number = trim(request()->param('number'));
        $where = ['enter_num'=>$number];
        if($this->usouquanModel->souquanSelect($where)[0]){
            return false;
        }else{
            return true;
        }
    }
    //接收注册信息
    public function zhuce()
    {
        $spwd = request()->param('spwd');
        $srepwd = request()->param('srepwd');
        $snumber = request()->param('snumber'); 
        $name =  trim(request()->param('name'));
        $pwd = md5(trim(request()->param('pwd')));
        if (($this->name() == '1') && 
            ($this->email()=='buzai1') &&
            $spwd==1 && 
            $srepwd == 1 &&
            $snumber == 1
        ){
            $ip = $_SERVER['REMOTE_ADDR'];
            if ($ip == '::1'){
                $ip = '127.0.0.1';
            } 
            $data1 = [
                'name' => $name,
                'ip' => $ip,
                'enter_ip'=>$ip,
                'enter_time'=>time(),
            ]; 
            $a=0;
           if ( is_int($this->userModel->userInsert($data1)) ){
                $where = ['name'=>$name];
                $id = json_decode($this->userModel->userSelect($where)[0]->id);              
                $data2 = [
                    [
                        'user_id' => $id,
                        'enter_type'=>'name',
                        'enter_num'=> trim(request()->param('name')),
                        'pwd' => $pwd,
                    ],
                    [
                        'user_id'=>$id,
                        'enter_type' => 'number',
                        'enter_num'=> trim(request()->param('number')),
                        'pwd' => $pwd,
                    ],
                    [
                        'user_id'=>$id,
                        'enter_type'=>'email',
                        'enter_num'=> trim(request()->param('email')),
                        'pwd' => $pwd,
                    ],
                ];
               $this->usouquanModel->souquanInsert($data2);
               $data3 =[
                    'user_id'  => $id,
                    'urole_id' => 1,
               ];
               $this->userroleModel->userroleInsert($data3);
               return json_encode(true);
           }
                
                return json_encode(false);   
        }else{
            
            return json_encode(false);
        } ;

    }
    //#####################登录#########################################
    //用户名/电话/邮箱
    public function num()
    {
        $num = trim(request()->param('num'));
        if(cache('name')==false){     
            $where = ['enter_num'=>$num];
            if($this->usouquanModel->souquanSelect($where)==null){
                return false;
            }else{
                return true;
            };
        }else{
            if($num != cache('name')){ 
                $where = ['enter_num'=>$num];
                if($this->usouquanModel->souquanSelect($where)==null){
                    return 0;
                }else{
                    return 1;
                };
            }else{
                return cache('name');  
            }            
        }
    }
    //密码
<<<<<<< HEAD
    public function dlpwd()
    { 
        $pwd = md5(trim(request()->param('dlpwd')));
        if (cache('pwd')==false) { 
            $num = trim(request()->param('num'));
            $where = ['enter_num'=>$num];
            if($re = $this->usouquanModel->souquanSelect($where)) {
                 if ($re[0]->pwd == $pwd){
                    return $re[0]->user_id;
                }else {
                    return false;
                }
            }
            return false;
        }else{
            if(is_int($this->num())){
                $num = trim(request()->param('num'));
                $where = ['enter_num'=>$num];
                if($re = $this->usouquanModel->souquanSelect($where)) {
                     if ($re[0]->pwd == $pwd){
                        return $re[0]->user_id;
                    }else {
                        return false;
                    }
                }
                return false;
            }else{
                return cache('pwd');
            };
        }       
    }
    //登录
    public function denglu()
    {
        if($this->dlpwd() != false ){
            $ji = request()->param('ji');
            if (cache('id')!=false){
                $user_id = cache('id');
            }else{
               $user_id = $this->dlpwd();  
            }
            $where = ['id'=>$user_id];
            $pwhere = ['user_id'=>$user_id];
            $pwd = $this->usouquanModel->souquanSelect($pwhere)[0];
            $re = $this->userModel->userSelect($where)[0];
            $rer = $re->userTomanyrole;
            $ree = $rer[0]->roleTomanyquanxian;
           if ($re->islock==0){
                session('id',$re->id);
                session('name',$re->name);
                session('touxiang',$re->touxiang);
                session('jifen',$re->jifen);
                session('vip',$re->vip);
                session('role',$rer[0]->role);
                session('quanxian',$ree[0]->quanxian);
                if ($ji == 1) {
                   cache('name',$re->name,3600*8);
                   cache('pwd',$pwd->pwd,3600*8); 
                   cache('id',$re->id,3600*8);
                }else{
                    return true;
                }
            }else{
                return false;
            }
       } else{
            return false;
       }   
    }
    //#####################退出##########################################
    //退出
    public function tuichu()
    {
        $tuichu = request()->param('tuichu');
        if($tuichu==1){
            session(null);
            return true;
        }
        return false;
=======
    public function pwd()
    {
    	$pwd = request()->param('pwd');
		$result = $this->validate(
    		[ 
    			'pwd' => $pwd,
    		],
    		[
    			'pwd' => 'min:6',
    		]);
    	if (true !== $result) {
    		return json_encode($result);
    	}
    }
    //确认密码
    public function repwd()
    {
    	$repwd = request()->param('repwd');
    	$pwd = request()->param('pwd');
    	if(strcmp($pwd,$repwd)){
    		return json_encode(false);
    	}else{
    		return json_encode(true);
    	}
>>>>>>> 30d92055e9c414d4b857afd0613e979a055c3b0c
    }
}